Original week4 homework by Eris (with minor changes)

load the data first

Here I simplify the file path because you already load the here package.

I also mute the Annex data because it is not needed.

library(here)
## here() starts at /Users/listianingrumr/Documents/CASA/GIS/Week 5/Real--GIS
#Globalgenderinequality <- read.csv("HDR21-22_Statistical_Annex_GII_Table.csv")
GIIdata <- read.csv("Composite_indices_complete_time_series.csv")
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
Worldshape <- st_read("World_Countries_(Generalized)/World_Countries__Generalized_.shp")
## Reading layer `World_Countries__Generalized_' from data source 
##   `/Users/listianingrumr/Documents/CASA/GIS/Week 5/Real--GIS/World_Countries_(Generalized)/World_Countries__Generalized_.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 251 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84

select the data from 2010 and 2019

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
GIIdata_2010_19<- GIIdata %>%
  dplyr::select (contains("gii_2010"),
                contains("gii_2019"),
                contains("country"))

creating new column

I also select gii_2010 and gii_2019 because we need the index pop up to make the advanced interactive map.

Different_2010_19 <-GIIdata_2010_19 %>%
  mutate(difference=(gii_2010 - gii_2019))%>%
  dplyr::select(gii_2010,gii_2019,difference,
                country)%>%
  
  arrange(desc(difference))

load the countrycode package and add country code table

Before we join the data, we have to add the countrycode because the shapefile use the different country name.

library(countrycode)
Different_2010_19$iso = countrycode(Different_2010_19$country,"country.name","iso2c")
## Warning in countrycode_convert(sourcevar = sourcevar, origin = origin, destination = dest, : Some values were not matched unambiguously: Arab States, East Asia and the Pacific, Europe and Central Asia, High human development, Latin America and the Caribbean, Low human development, Medium human development, South Asia, Sub-Saharan Africa, Very high human development, World

notes: fyi because you choose country name as the left join id, this warning appears:

Warning: Some values were not matched unambiguously: Arab States, East Asia and the Pacific, Europe and Central Asia, High human development, Latin America and the Caribbean, Low human development, Medium human development, South Asia, Sub-Saharan Africa, Very high human development, World.

It appears because the “naming” system in the csv file is different from the packages, so it cannot add code for some of the countries’ names from the list.

join the data

library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(stringr)
giifinalmap <- Worldshape %>% 
  clean_names() %>% 
  left_join(., Different_2010_19, by = c("country" = "country"))

plot

library(tmap)
library(tmaptools)
  tm_shape(giifinalmap) + 
  tm_polygons("difference", 
              style="pretty",
              palette="Blues",
              midpoint=NA,
              #title="Gender Inequality Difference 2010 & 2019",
              alpha = 0.7) + 
  tm_compass(position = c("left", "bottom"),type = "arrow") + 
  tm_scale_bar(position = c("left", "bottom")) +
  tm_layout(title = "Gender Inequality Difference 2010 & 2019", legend.position = c("right", "bottom")) +
  tmap_options(max.categories = 115)
## Scale bar set for latitude km and will be different at the top and bottom of the map.

Week5 homework: Map making

static map

tmap_mode("plot")
## tmap mode set to plotting
# set the breaks
# for our mapped data
breaks = c(-0.1, 0, 0.1, 0.2, 0.3, 0.4) 

# plot each map
tm1 <- tm_shape(giifinalmap) + 
  tm_polygons("difference", 
              breaks=breaks,
              palette="PuBu")+
  tm_legend(show=FALSE)+
  tm_layout(frame=FALSE)+
  tm_credits("(a)", position=c(0,0.85), size=1)

legend <- tm_shape(giifinalmap) +
    tm_polygons("difference",
                palette="PuBu") +
    tm_scale_bar(position=c(0.2,0.04), text.size=0.6)+
    tm_compass(north=0, position=c(0.65,0.6))+
    tm_layout(legend.only = TRUE, legend.position=c(0.2,0.25),asp=0.1)+
    tm_credits("(b) difference in gender inequality ", 
               position=c(0.2,0.0), size=0.6)
  
t=tmap_arrange(tm1, legend, ncol=1)

t
## Warning: Breaks contains positive and negative values. Better is to use
## diverging scale instead, or set auto.palette.mapping to FALSE.
## Scale bar set for latitude km and will be different at the top and bottom of the map.

basic interactive map

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(giifinalmap) + 
  tm_polygons("difference", breaks=breaks) 
## Variable(s) "difference" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

advanced interactive map

# library for pop up boxes (setting 3 pop up boxes:gii_2010, gii_2019 and difference)
library(leafpop)
library(leaflet)

#avoid the geometry for our pop up boxes
popup_gii2010 <-giifinalmap %>%
  st_drop_geometry()%>%
  dplyr::select(`gii_2010`, country)%>%
  popupTable()

popup_gii2019 <-giifinalmap %>%
  st_drop_geometry()%>%
  dplyr::select(`gii_2019`, country)%>%
  popupTable()

popup_difference <-giifinalmap %>%
  st_drop_geometry()%>%
  dplyr::select(`difference`, country)%>%
  popupTable()

tmap_mode("view")
## tmap mode set to interactive viewing
## set the colour palettes using our previous breaks

breaks2=c(0,0.3,0.5,0.7,0.9,1)

pal1 <- giifinalmap %>%
  colorBin(palette = "YlOrRd", domain=.$`gii_2010`, bins=breaks2)

pal2 <- giifinalmap %>%
  colorBin(palette = "YlOrRd", domain=.$`gii_2019`, bins=breaks2)

pal3 <- colorBin(palette = "YlOrRd", 
                domain=giifinalmap$`difference`, bins=breaks)

map<- leaflet(giifinalmap) %>%
  # add basemap options
  addTiles(group = "OSM (default)") %>%
  addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
  addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
  addProviderTiles(providers$CartoDB.Positron, group = "CartoDB")%>%

  #add our polygons, linking to the tables we just made
  addPolygons(color="white", 
              weight = 2,
              opacity = 1,
              dashArray = "3",
              popup = popup_gii2010,
              fillOpacity = 0.7,
              fillColor = ~pal1(`gii_2010`),
              group = "gii_2010") %>%
  
  addPolygons(fillColor = ~pal2(`gii_2019`), 
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              popup = popup_gii2019,
              fillOpacity = 0.7,
              group = "gii_2019") %>%

  addPolygons(color="white", 
              weight = 2,
              opacity = 1,
              dashArray = "3",
              popup = popup_difference,
              fillOpacity = 0.7,
              fillColor = ~pal3(`difference`),
              group = "difference") %>%

  # add a legend

  addLegend(pal = pal3, values = ~`difference`, 
            group = c("gii_2010","gii_2019","difference"), 
            position ="bottomleft", title = "gender inequality index") %>%
  # specify layers control
  addLayersControl(
    baseGroups = c("OSM (default)", "Toner", "Toner Lite", "CartoDB"),
    overlayGroups = c("gii_2010", "gii_2019","difference"),
    options = layersControlOptions(collapsed = FALSE)
  )
## plot the map
map